package com.dongnam.quanly.web.system;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.util.PropertiesUtil;

public class AppConfig {

	private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);

	public static final String VERSION = "1.0.0";

	private static final String DEFAULT_APP_CONFIG_NAME = "/lrd_console.properties";
	private static final String DEFAULT_LRD_LOG_PROPERTIES = "/lrd_log.properties";
	private static Properties appConfig;
	private static String appConfigFilePath;

	public static String getAppConfigFilePath() {
		return appConfigFilePath;
	}

	public static synchronized void loadAppConfig() {
		logger.info("Loading application configuration from [{}]", appConfigFilePath);
		appConfig = new Properties();

		Properties newConfig = new Properties();
		File configFile = new File(appConfigFilePath);
		try {
			newConfig.load(new BufferedReader(new FileReader(configFile)));
			appConfig = newConfig;
			logger.info("Loaded from {} [{}]", appConfigFilePath, AppConfig.getAppConfig());
		} catch (FileNotFoundException e) {
			try {
				newConfig.load(AppConfig.class.getClassLoader().getResourceAsStream(appConfigFilePath));
				appConfig = newConfig;
				logger.info("Loaded from {} [{}]", appConfigFilePath, AppConfig.getAppConfig());
			} catch (IOException e1) {
				logger.error("Unable to find application configuration file at location: [{}]", appConfigFilePath, e1);
				logger.info("Program exiting....");
				System.exit(-1);
			}
		} catch (IOException e) {
			logger.error("Unable to read application configuration file at location: [{}]", appConfigFilePath, e);
			logger.info("Program exiting....");
			System.exit(-1);
		}
	}

	public static void loadAppConfig(String filePath) {
		appConfigFilePath = filePath;
		loadAppConfig();
	}

	public static void loadLogConfig() {
		String logFilepath = AppConfig.getLog4jDir();
		logger.info("Loading logger configuration from [{}]", logFilepath);

		Properties logProperties = new Properties();
		try {
			logProperties.load(new FileInputStream(logFilepath));
			PropertyConfigurator.configure(logProperties);
			logger.info("Loaded: [{}]", logProperties);
		} catch (FileNotFoundException e) {
			try {
				logProperties.load(AppConfig.class.getClassLoader().getResourceAsStream(logFilepath));
				PropertyConfigurator.configure(logProperties);
				logger.info("Loaded: [{}]", logProperties);
			} catch (IOException e1) {
				logger.error("Unable to find logger configuration file at location: [{}]", logFilepath, e1);
			}

		} catch (IOException e) {
			logger.error("Unable to read logger configuration file at location: [{}]", logFilepath, e);
		}
	}

	public static Properties getAppConfig() {
		return appConfig;
	}

	public static String getLog4jDir() {
		return PropertiesUtil.parsePropertiesValue(appConfig.getProperty("log4j.filepath", DEFAULT_LRD_LOG_PROPERTIES));
	}

	public static String getHibernateConfig() {
		return PropertiesUtil.parsePropertiesValue(appConfig.getProperty("hibernate.config.filepath",
				"hibernate.cfg.xml")); // null, will load conf/hibernate.cfg.xml
										// by default
	}

	public static String getManagementAppMBeanName() {
		return appConfig.getProperty("jmx.name");
	}

	public static String getDefaultAppConfigLocation() {
		return PropertiesUtil.parsePropertiesValue(DEFAULT_APP_CONFIG_NAME);
	}

	public static String getMyName() {
		return appConfig.getProperty("sys.prop.app.name");
	}

	public static String getWebDateFormat() {
		return appConfig.getProperty("web.date.format");
	}

	public static String getWebDefaultLang() {
		return appConfig.getProperty("web.default.lang");
	}

	public static String getWebDefaultCountry() {
		return appConfig.getProperty("web.default.country");
	}

	public static String getImsiMaxLength() {
		return appConfig.getProperty("imsi.maxlength");
	}

	public static String getMsisdnMaxLength() {
		return appConfig.getProperty("msisdn.maxlength");
	}

	public static String getVirtualMsisdnMaxLength() {
		return appConfig.getProperty("vmsisdn.maxlength");
	}

	public static String getFileExtensionPattern() {
		return appConfig.getProperty("file.extension");
	}

	public static String getNameLanguageMaxLength() {
		return appConfig.getProperty("nameLanguage.maxlength");
	}

	public static String getDesLanguageMaxLength() {
		return appConfig.getProperty("desLanguage.maxlength");
	}

	public static String getMccMaxLength() {
		return appConfig.getProperty("mcc.maxlength");
	}

	public static String getMncMaxLength() {
		return appConfig.getProperty("mnc.maxlength");

	}

	public static String getApnMaxLength() {
		return appConfig.getProperty("apn.maxlength");
	}

	public static String getGrowlShow() {
		return appConfig.getProperty("growl.timeshow");
	}
}
